"""新发地菜价，使用多线程爬取"""
import csv
from concurrent.futures import ThreadPoolExecutor

import requests


def get_one_page(n):
    url = "http://www.xinfadi.com.cn/getPriceData.html"
    data = {"limit": 20,
            "current": n}
    html = requests.post(url, data=data).json()
    list1 = html["list"]
    with open("./data.csv", "a", encoding="utf-8", newline="") as f:
        csvwriter = csv.writer(f)
        for i in list1:
            a = (i["prodName"], i["avgPrice"], i["place"], i["unitInfo"])
            csvwriter.writerow(a)
    print("第%d页提取完成" % n)


with ThreadPoolExecutor(50) as t:
    for n in range(1, 50):
        t.submit(get_one_page, n)
